package com.hnkywl.sts.dao.xsgl;

import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.xsgl.Kh;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * 赎货价计算dao
 */
@Repository
public class ShjDao extends SimpleHibernateTemplate{
    private KhDao khDao;

    public Map<String,Object> calculate(Long khId) {
        return calculate(khId,new Date());
    }

    public Map<String,Object> calculate(Long khId,Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String jsrq = df.format(date);
        String sql = "select nvl(shj,0)-nvl(lxjt,0) as \"hsj\",lxjt as \"yhjg\",jsfs || '-' || lxjt as \"jsfs\" from T_BIZ_SHJ where kh_id = ? and to_char(jsrq,'yyyyMMdd') = ?";
        SQLQuery sqlQuery = createSQLQuery(sql);
        sqlQuery.setParameter(0,khId);
        sqlQuery.setParameter(1,jsrq);
        sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        Map<String, Object> result = (Map<String, Object>) sqlQuery.uniqueResult();
        if(result == null) {
            Kh kh=khDao.get(khId);
            Cgd cgd = kh.getCgd();
            Double sl = Double.valueOf(cgd.getSl());
            result = new HashMap<String, Object>();
            if(kh!=null) {
                result.put("hsj", Math.round((kh.getWsdj() * 10000 * (1 + sl / 100))) / 10000.0);
                result.put("yhjg", BigDecimal.valueOf(0l));
                result.put("jsfs",kh.getWsdj() + " * (1 + " + sl + " / 100)");
            }else{
                result.put("hsj", BigDecimal.valueOf(0l));
                result.put("yhjg", BigDecimal.valueOf(0l));
                result.put("jsfs","0");
            }
        }
        return result;
    }

    public void executeShj(Long khId) {
        Connection con = null;
        CallableStatement call = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call R_BIZ_SHJ_DAY(?)}");
            call.setLong(1, khId);
            call.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResource(null,call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }
}
